#!/usr/bin/env bash
# yitong.bai@qq.com
# Library for downloading functions

function DownloadDockerRegistryInstallations() {
  local dataPath="${1}"

  mkdir -p "${dataPath}" && cd "${dataPath}" 

  docker pull registry:2 &> /dev/null
  docker save registry:2 -o registry.tar &> /dev/null
}

# 执行kubespray中关于download的task，下载所有安装k8s所需要的软件包
function DownloadKubernetesInstallations() {
  local srcDir="$1"
  local dataPath="$2"

  cd "${srcDir}" || return 1
  ansible-playbook -i inventory/sample/inventory.ini  \
  -e local_release_dir="${dataPath}" --tags download cluster.yml
}

# 下载kubespray源码，参数1是下载的目标目录，依赖git指令
function DownloadKubesprayInstallations() {
  local dataPath="${1}"

  cd "${dataPath}" || return 1
  rm -fr kubespray/
  git clone https://github.com/kubernetes-sigs/kubespray.git &> /dev/null
}

# 下载Ansible安装包
function DownloadAnsibleInstallations() {
  local dataPath="${1}"
  local requirements="${2}"

  mkdir -p "${dataPath}" && cd "${dataPath}"

  python3 -m pip install -q pip2pi

  cd "${dataPath}" || return 1
  pip2tgz path -r "${requirements}" > /dev/null
  dir2pi path/ > /dev/null
}